package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.http.BusinessException;
import com.example.demo.common.http.RestResult;
import com.example.demo.model.UserInfo;
import com.example.demo.model.command.UserInfoSaveCommand;
import com.example.demo.model.command.UserInfoUpdateCommand;
import com.example.demo.model.converter.UserInfoConverter;
import com.example.demo.model.resp.UserInfoDetailResp;
import com.example.demo.model.resp.UserInfoPageResp;
import com.example.demo.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author chenzl
 * @since 2023-10-11
 */
@RestController
@RequestMapping("/userInfo")
@Api(tags = "系统用户表")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/save")
    @ApiOperation(value = "新增系统用户表")
    public RestResult<String> save(@RequestBody @Validated UserInfoSaveCommand command) {
        UserInfo userInfo = UserInfoConverter.INSTANCE.convert(command);
        userInfoService.save(userInfo);
        return RestResult.getSuccessResult(String.valueOf(userInfo.getId()));
    }

    @PostMapping("/removeById")
    @ApiOperation(value = "根据id删除系统用户表")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "String")
    )
    public RestResult<Boolean> removeById(String id) {
        boolean success = userInfoService.removeById(Long.parseLong(id));
        return RestResult.getSuccessResult(success);
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新系统用户表")
    public RestResult<Boolean> updateById(@RequestBody @Validated UserInfoUpdateCommand command) {
        UserInfo userInfo = UserInfoConverter.INSTANCE.convert(command);
        boolean success = userInfoService.updateById(userInfo);
        return RestResult.getSuccessResult(success);
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查询系统用户表")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "String")
    )
    public RestResult<UserInfoDetailResp> getById(String id) {
        UserInfo userInfo = userInfoService.getById(Long.parseLong(id));
        if (null == userInfo) {
            throw new BusinessException("id不存在");
        }
        UserInfoDetailResp resp = UserInfoConverter.INSTANCE.convert2DetailResp(userInfo);
        return RestResult.getSuccessResult(resp);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询系统用户表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "Integer", paramType = "query")
    })
    public RestResult<Page<UserInfoPageResp>> page(Integer pageNum, Integer pageSize) {
        Page<UserInfo> page = userInfoService.page(new Page<>(pageNum, pageSize));
        Page<UserInfoPageResp> resp = UserInfoConverter.INSTANCE.convert2PageResp(page);
        return RestResult.getSuccessResult(resp);
    }
}
